package cn.tedu.jsd2210.csmall.product.controller;

import cn.tedu.jsd2210.csmall.product.pojo.dto.AlbumUpdateDto;
import cn.tedu.jsd2210.csmall.product.pojo.dto.CategoryAddNewDto;
import cn.tedu.jsd2210.csmall.product.pojo.dto.CategoryUpdateDto;
import cn.tedu.jsd2210.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.jsd2210.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.jsd2210.csmall.product.service.ICategoryService;
import cn.tedu.jsd2210.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/categories")
@Api(tags="03.类别管理模块")
@Validated
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;
//http://localhost:9080/categories/add-new
    @ApiOperation("添加类别")
    @PostMapping("/add-new")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid CategoryAddNewDto categoryAddNewDto){
            log.debug("开始处理添加类别的请求,参数:{}",categoryAddNewDto);
            categoryService.addNew(categoryAddNewDto);
            return JsonResult.ok();
    }
//
//    http://localhost:9080/categories/{?}/delete?
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除类别")
    @ApiOperationSupport(order = 200)
//    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value = "类别ID",required = true,dataType = "long",example = "100")
//        ,@ApiImplicitParam(name="username",value = "测试多参数用户名",required = true,dataType = "string")
//    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1L,message = "请提交有效的ID值!") Long id){
        log.debug("开始处理根据id删除类别的请求,参数:{}",id);
        categoryService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改类别")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name="id",value = "类别ID",required = true,dataType = "long",example = "100")
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1L,message = "请提交有效的ID值!") Long id,
                                           @Valid CategoryUpdateDto categoryUpdateDto){
        log.debug("开始处理根据id{}修改类别的请求,新数据:{}",id,categoryUpdateDto);
        categoryService.updateInfoById(categoryUpdateDto,id);
        return JsonResult.ok();
    }
    
    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
//    @ApiImplicitParams({
    @ApiImplicitParam(name="id",value = "类别ID",required = true,dataType = "long",example = "100")
//        ,@ApiImplicitParam(name="username",value = "测试多参数用户名",required = true,dataType = "string")
//    })
    public JsonResult<Void> setEnable(@PathVariable @Range(min = 1L,message = "请提交有效的ID值!") Long id){
        log.debug("开始处理根据修改类别状态的请求,参数:{}",id);
        categoryService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 311)
//    @ApiImplicitParams({
    @ApiImplicitParam(name="id",value = "类别ID",required = true,dataType = "long",example = "100")
//        ,@ApiImplicitParam(name="username",value = "测试多参数用户名",required = true,dataType = "string")
//    })
    public JsonResult<Void> setDisable(@PathVariable @Range(min = 1L,message = "请提交有效的ID值!") Long id){
        log.debug("开始处理根据修改类别状态的请求,参数:{}",id);
        categoryService.setDisable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/display")
    @ApiOperation("展示类别")
    @ApiOperationSupport(order = 320)
//    @ApiImplicitParams({
    @ApiImplicitParam(name="id",value = "类别ID",required = true,dataType = "long",example = "100")
//        ,@ApiImplicitParam(name="username",value = "测试多参数用户名",required = true,dataType = "string")
//    })
    public JsonResult<Void> setDisplay(@PathVariable @Range(min = 1L,message = "请提交有效的ID值!") Long id){
        log.debug("开始处理根据修改类别展示状态的请求,参数:{}",id);
        categoryService.setDisplay(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/undisplay")
    @ApiOperation("禁止展示类别")
    @ApiOperationSupport(order = 322)
//    @ApiImplicitParams({
    @ApiImplicitParam(name="id",value = "类别ID",required = true,dataType = "long",example = "100")
//        ,@ApiImplicitParam(name="username",value = "测试多参数用户名",required = true,dataType = "string")
//    })
    public JsonResult<Void> setUnDisplay(@PathVariable @Range(min = 1L,message = "请提交有效的ID值!") Long id){
        log.debug("开始处理根据修改类别禁止展示状态的请求,参数:{}",id);
        categoryService.setUnDisplay(id);
        return JsonResult.ok();
    }



    //http://localhost:9080/categories/list-by-parent
    @GetMapping("/list-by-parent")
    @ApiOperation("根据父级查询子级列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<CategoryListItemVO>> listByParentId( Long parentId){
        log.debug("开始处理根据查询类别列表的请求.无参数");
        List<CategoryListItemVO> list = categoryService.listByParentId(parentId);
        return JsonResult.ok(list);
//        JsonResult jsonResult = JsonResult.ok();
//        jsonResult.setData(list);
//        return jsonResult;
    }
//
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查找类别")
    @ApiOperationSupport(order = 410)
    public JsonResult<CategoryStandardVO> getStandardById(
            @PathVariable @Range
                    (min = 1L,message = "请提交有效的ID值!")
                    Long id){
        log.debug("开始处理根据ID:{}查询类别列表的请求",id);
        CategoryStandardVO queryResult = categoryService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }
}
